﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.UI.HtmlControls;

using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.warehouse
{
    public partial class stockOutOrder : BaseAdmin
    {
        readonly OrderAccount theOrderAccount = new OrderAccount();
        readonly DALCardType theCardType = new DALCardType();
        readonly DALGoods theGoods = new DALGoods();
        readonly DALCard theCard = new DALCard();
        readonly DALStockOrder theStockOrder = new DALStockOrder();
        readonly DALStockOrderGoods theStockOrderGoods = new DALStockOrderGoods();
        readonly dalCompany theCompany = new dalCompany();

        tbStockOrder oStockOrder;
        tbCompany oCompany;



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                base.ValidateLoginRule(Gift.Model.ENUM.ERule.StockOrderAdd);

                if (Request.QueryString["action"] == "del")
                {

                    this.theOrderAccount.DeleOrderAccount(Request.QueryString["code"], int.Parse(Request.QueryString["id"]));
                    UserCardCodeList.RemoveAll(item => item.UserCardId == Request.QueryString["id"]);
                    ExtAspNet.Alert.Show("删除成功！");

                }
                this.BindSupplyCompanyList();
                this.BuildForm();
                this.BindAddGoodsGrid();
            }

        }

        void BuildForm()
        {
            if (Session["stockOrderCode"] != null)
            {
                this.txtStockOrderCode.Text = Session["stockOrderCode"].ToString();
            }

          
            this.txtStockOrderCode.Text = DateTime.Now.ToString("yyyyMMddhhmmssffff");
            Session["stockOrderCode"] = this.txtStockOrderCode.Text;
  
        }

        void BindSupplyCompanyList()
        {
            string sWhere = "";

            DataTable table = theCompany.GetList("id!=0").Tables[0];
            this.ddlCompanyList.DataTextField = "name";
            this.ddlCompanyList.DataValueField = "id";
            this.ddlCompanyList.DataSource = table;
            this.ddlCompanyList.DataBind();

            this.ddlCompanyList.Label = "选择采购商";
            this.ddlCompanyList.Items.Insert(0, new ExtAspNet.ListItem("选择采购商", "-1"));

                this.ddlCompanyList.SelectedValue = "-1";



        }

        private void BindAddGoodsGrid()
        {
            DataTable table = theStockOrderGoods.GetListByStockOrderCode(this.txtStockOrderCode.Text.Trim()).Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();
        }



        protected void btnSubmitOrder_Click(object sender, EventArgs e)
        {
            //if (string.IsNullOrEmpty(Request.QueryString["id"]))
            //{
            oStockOrder = new tbStockOrder();
            oStockOrder.address = this.txtAddress.Text.Trim();

            oStockOrder.creater = int.Parse(base.AdminUserId);
            oStockOrder.createTime = DateTime.Now;
            oStockOrder.goodsToDate = DateTime.Parse(this.dpkSendDate.Text);
            oStockOrder.linker = this.txtLinker.Text;
            oStockOrder.linkerPhone = this.txtLinkerPhone.Text;
            oStockOrder.remark = this.txtRemark.Text.Trim();
            oStockOrder.stockStates = 0;

             oStockOrder.companyId = 0;
             oStockOrder.stockCompanyId = int.Parse(this.ddlCompanyList.SelectedItem.Value);

            oStockOrder.stockOrderCode = this.txtStockOrderCode.Text.Trim();
            theStockOrder.Add(oStockOrder);
            theStockOrder.UpdateStockGoodsFreeze(oStockOrder.stockOrderCode);

            Session["stockOrderCode"] = null;
            ExtAspNet.Alert.Show("提交订单成功！请记住订单编号(" + this.txtStockOrderCode.Text.Trim() + ")以便查询！", "", "window.location.href='stockOrderList.aspx'");

        }

        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table = theStockOrderGoods.GetListByStockOrderCode(this.txtStockOrderCode.Text.Trim()).Tables[0];
            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            Grid1.DataSource = view1;
            Grid1.DataBind();
        }




        protected void Window1_Close(object sender, EventArgs e)
        {
            this.BindAddGoodsGrid();
            // ExtAspNet.Alert.Show("postback回发窗口！");
        }

        protected void Window2_Close(object sender, EventArgs e)
        {
            this.BindAddGoodsGrid();
            // ExtAspNet.Alert.Show("postback回发窗口！");
        }

        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName == "deleted")
            {
                theStockOrderGoods.Delete(int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString()));

                ExtAspNet.Alert.Show("商品删除成功！");
                this.BindAddGoodsGrid();
            }
        }

        protected void ddlCompanyList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlCompanyList.SelectedItem.Value != "-1")
            {
                oCompany = theCompany.GetModel(int.Parse(this.ddlCompanyList.SelectedItem.Value));
                this.txtAddress.Text = oCompany.Provence + "," + oCompany.City + "," + oCompany.Address;
                this.txtLinker.Text = oCompany.Linker;
                this.txtLinkerPhone.Text = oCompany.Mobile;
            }
        }


    }
}